package com.framework.core.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.annotation.WebServlet;

import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.ConstPool;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.StringMemberValue;

public class InjectServletUrlPattern {
	private static InjectServletUrlPattern injectServletUrlPattern;

	public static InjectServletUrlPattern getInstance() {
		if (null == injectServletUrlPattern) {
			injectServletUrlPattern = new InjectServletUrlPattern();
		}
		return injectServletUrlPattern;
	}

	public void doInject(String urlPattern, Object obj)
			throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException,
			IllegalArgumentException, InvocationTargetException, NoSuchFieldException, NotFoundException {

		Class<?> clazz = obj.getClass();
		WebServlet annotation = clazz.getAnnotation(WebServlet.class);

		String[] s = new String[] { urlPattern };
		for (String url : s) {
			System.err.println("UrlPattern:" + url);
		}
		String[] urlPatterns = annotation.urlPatterns();
		for (String string : urlPatterns) {
			System.err.println("WebServlet:" + string);
		}
		// setTargetBean(annotation.getClass(), "urlPatterns", obj, s);
		inject(obj);
	}

	// 依赖注入
	public static void setTargetBean(Class<?> clazz, String targetAttr, Object obj, Object targetAttrValue)
			throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException,
			InvocationTargetException, NoSuchFieldException {
		System.err.println("正在注入----类：" + targetAttrValue.getClass() + "---属性：" + targetAttr);
		// 不通过set方法注入field值
		Field field = clazz.getDeclaredField(targetAttr);
		field.setAccessible(true);
		field.set(obj, targetAttrValue);
	}

	public void inject(Object obj) throws NotFoundException, ClassNotFoundException {

		ClassPool pool = ClassPool.getDefault();
		// 解决classnofound异常
		ClassClassPath classPath = new ClassClassPath(this.getClass());
		pool.insertClassPath(classPath);
		// 获取需要修改的类
		CtClass ct = pool.get("com.framework.core.servlet.InitServlet");

		ClassFile cf = ct.getClassFile();
		ConstPool cp = cf.getConstPool();

		AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
		Annotation a = new Annotation("WebServlet", cp);
		a.addMemberValue("urlPatterns", new StringMemberValue("*.action", cp));
		attr.setAnnotation(a);
		cf.addAttribute(attr);
		cf.setVersionToJava5();

		Class<?> clazz = obj.getClass();
		WebServlet annotation = clazz.getAnnotation(WebServlet.class);
		String[] urlPatterns = annotation.urlPatterns();
		for (String string : urlPatterns) {
			System.err.println("NewWebServlet:" + string);
		}
	}

}
